package com.cdivtc.management.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


@Component
public class PdfToImage {
    /**
     * 转换全部的pdf

     * @param type 图片类型
     */


    public   List<ByteArrayOutputStream>  pdf2png(InputStream fileinput,String type) {
        // 将pdf装图片 并且自定义图片得格式大小
        List<ByteArrayOutputStream> listinput = new ArrayList();
        try {
            PDDocument doc = PDDocument.load(fileinput);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();

            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
                // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                ImageIO.write(image, type,outputStream);
                listinput.add(outputStream);



            }

            return listinput;
        } catch (IOException e) {
            e.printStackTrace();
        }


        return  null;
    }



}
